package com.auctionhaus

class Bid {

    Double amount
    Date date
    static Double incrementRate = 0.50
    Customer buyer

   static belongsTo = [buyer:Customer, listing:Listing]            //B5

    static constraints = {
        amount(nullable: false, validator: {val, obj ->

                                        if (obj.listing != null) {
                                            def maxPrice = Listing.findByListName(obj.listing?.listName)?.getMaxBidPrice()
                                            if (!maxPrice)
                                                return true
                                            else
                                                return val >= maxPrice + incrementRate
                                        }
                                        return false

        })
        buyer(blank: false, nullable:false)
        date(nullable: false)
    }

    static mapping = {
        listing cascade: 'refresh'
    }
}
